package com.test;

import com.alibaba.fastjson.JSON;
import com.api.gateway.util.AESUtils;
import com.api.gateway.util.RSAUtils;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by hao.g on 18/4/4.
 */
public class URLGenerator {
    //客户端公私钥
    private static final String client_privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJmzTOEU36iO3Y2zdIiepqcrdXfWxyMM48nDf-CQWi4B4tGaUwLaZ8aLPxaBpp-C1gtQQvoCgjP8z4EPcd4e-CUYT0199O_-ig4zDv4SXmlqdr7uiDFy8y4XslASnxRXVeyiy07l9YDnS-GenYZbDsJrloAF_Te83s91AkXjz4sVAgMBAAECgYEAk_PC94_OVEViA_tRHAoMRjNRWlCHVLgdtaTZPeP2yv7JxhaV_dMbvkVgL1wSds3NGQrjziIID982J_gkFjsdhaedPHDh20JKqr-thYPGB9DUYAh-Xo4KoxVwDIPzrh8itP98NbFjdkD0UWmGjwEBsSolH9LqoitTX66E6dvCbekCQQDdNPBaSIRfQa3s709Nf0he6bHlWbAv2Hg0MOEuwWJIXRO1t4M4vUSHadWx2Oqe3dTSu0YwNoSTiMPYPTdeEuDLAkEAseAqGRKsD9euuIprEaqIFMcqWNRbeQmtw620o2BQCAFYHBJWjK2mbGMlEY65hbK3i7tUngzuNHDGaqbQuaUnnwJBAJwf1A_0pLDSm13h3XfWAQ80I6G7NnXec6Bqa1Rjmul2AAY-OnheZTKpyPBYJ-fuq1n8YWJ_5_x1B7zkH-LeshcCQHlanl4Mxybn12Q_y1x6I-1JFDMWbnHdkJd3402zX-aWRC6itDm2H1zDJ38bCKeWjnLek5Rg7gMJDdWlibGeY7ECQAgkcyHH1SIIJ-nT3DwJ-zfmi-LaCLNh43XAfUbpFXqv9efkal1Cm5gIi_4k-GfUZ1pxmESKzcbQaewxzvAQ9lY";

    //服务端公私钥
    private static final String server_publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcRZaiz50lAlWMhX6aESRwNEh4x12L8gpcZL7s2LEydlWJUqecGuLX56LwwP8VulJt9xJ9OH80Ehzeed2gGQuy468tAt0fDz8BKbn5f_Wyaxcuj_9hVA5QAroVGTOKRWcRbMsWIyUFF-BGv9ZR_MlQS41VUyVsluklEM90KY_qFwIDAQAB";

    public static void main(String args[]) throws InvalidKeySpecException, NoSuchAlgorithmException {
        Map<String, String> map = new HashMap<>();
        map.put("bizNo", "B" + formatTime() + getUUID());
        map.put("code", "CIB");
        map.put("loginName", "shuli0910");
        map.put("loginPasswd", "shuli0910");
        String data = JSON.toJSONString(map);

        //AES秘钥
        String aesKey = AESUtils.getRandomKey();
        //对称加密数据
        String ciphertext = AESUtils.encrypt(data, aesKey);

        //服务端公钥加密aes的密钥
        String aesKeyCipher = RSAUtils.publicEncrypt(aesKey, RSAUtils.getPublicKey(server_publicKey));

        //客户端私钥签名
        String signCipher = RSAUtils.privateEncrypt(aesKeyCipher, RSAUtils.getPrivateKey(client_privateKey));

        System.out.println("http://localhost:8080/gateway?api=custom.cspider&data=" + ciphertext +
                "&charset=UTF-8&encKey=" + aesKeyCipher +
                "&sign=" + signCipher +
                "&timestamp=" + System.currentTimeMillis() +
                "&version=1.0");
    }

    private static String formatTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }

    private static String getUUID(){
        String uuidNo  = UUID.randomUUID().toString().replaceAll("-", "");
        return uuidNo.substring(0, 17);
    }
}
